package com.ljm.boot.rabbitmq.config;

import com.ljm.boot.rabbitmq.constant.*;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/***
 * direct 模式会造成路由 RoutingKey 太多, 而实际开发中往往是按照某个规则来进行路由匹配的,
 * RabbitMQ 提供了 Topic 模式/主题模式来适应这种需求.
 * Topic 模式是 direct 模式上的一种扩展/叠加, 扩展/叠加了模糊路由 RoutingKey 的模式,
 * 可以理解为是模糊的路由匹配模式
 */


public class TopicConfig {

    @Bean
    public Queue queue_topic01() {
        return new Queue(RabbitMQConstant.TOPIC_QUEUE_1);
    }

    @Bean
    public Queue queue_topic02() {
        return new Queue(RabbitMQConstant.TOPIC_QUEUE_2);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(RabbitMQConstant.TOPIC_EXCHANGE);
    }

    @Bean
    public Binding binding_topic1() {
        //将队列queue1和交换机进行绑定
        return BindingBuilder.bind(new Queue(RabbitMQConstant.TOPIC_QUEUE_2)).to( new TopicExchange(RabbitMQConstant.TOPIC_EXCHANGE)).with(RabbitMQConstant.topic_routingKey_1);
    }

    @Bean
    public Binding binding_topic2() {
        //将队列queue1和交换机进行绑定
        return BindingBuilder.bind(new Queue(RabbitMQConstant.TOPIC_QUEUE_2)).to( new TopicExchange(RabbitMQConstant.TOPIC_EXCHANGE)).with(RabbitMQConstant.topic_routingKey_2);
    }

}
